
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TrainingPanel.java
 *
 * Created on Jan 15, 2012, 12:54:31 PM
 */
/**
 *
 * @author Figueroa
 */
public class TrainingPanel extends javax.swing.JPanel implements MouseListener, MouseMotionListener {

    /** Creates new form TrainingPanel */
    public TrainingPanel() {
        initComponents();
        board.addMouseListener(this);
        board.addMouseMotionListener(this);
    }
    public PlayBoard board = new PlayBoard();
    public String player;
    public List<Integer>ExcerciseList;
    private String BoardStatus = "";
    Timer_panel timer = new Timer_panel(this);
    private String Registry = ""; 
    private List<char[]> Movimientos = new ArrayList<char[]>();
    private List<String> Registros = new ArrayList<String>();
    private List<String> Solutions = new ArrayList<String>();
    private int IndexMovimientos = 1;
    private int turno =0;
    private int ejercicio = 0;
    private int tiempo;
    private int currentEX = 0;
    
    
    
    //Carga un ejercicio en el Panel
    public void load_excercise(int id, int time){
     init_variables();
     ejercicio=id;
     tiempo = time;
     //board.addListeners();
     board.addMouseListener(this);
     board.addMouseMotionListener(this);
     load_info_excercise(id);
     load_Solutions(id);
     board.setSize(450, 450);
     board.paint(this.getGraphics());
     PanelBoard.removeAll();
     clockPanel.removeAll();
     RepaintPanelBoard();
     PanelBoard.add(board,null);
     clockPanel.add(timer,null);
        try {
            timer.Start(time);
        } catch (Exception ex) {
          
                        Logger.getLogger(TrainingPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void load_info_excercise(int id){
    ConectorBD conexion = new ConectorBD();
    conexion.conectar();
        List<String> Information = conexion.Get_ExcercicesByID(id);
        nametxt.setText(Information.get(0));
        descriptiontxt.setText(Information.get(1));
        BoardStatus = Information.get(2);
        turno = Integer.parseInt(Information.get(3));
        board.whiteTurn = indextobool(turno);
        char[] piece_ex = BoardStatus.toCharArray().clone();
        Movimientos.add(piece_ex.clone());
        Registros.add("");
        IndexMovimientos++;
        board.piece = piece_ex;
        board.update(this.getGraphics());
        int start = Integer.parseInt(Information.get(3));
        
    }
    
    
    
    private boolean indextobool(int i){
     boolean resul = false;
       if(turno == 1)
           resul=true;
    return resul;
    }
    
    public boolean Who_plays(int i){
       boolean resul = false;
       if(turno == 1)
           resul=true;
        
       if(i%2!=0){
          return resul;
        }
       else{
         return !resul;
       }
    }
    
    public void load_Solutions(int id){
     ConectorBD conexion = new ConectorBD();
        conexion.conectar();
       Solutions = conexion.GET_SolutionByID(id);
    
    }
    private void create_stats(int ExID,String time,int resul,String answer){
     ConectorBD conexion = new ConectorBD();
     conexion.conectar();
     conexion.Create_stat(ExID, player,time,resul,answer);
    }
    
    public void Timesup(){
     for(int i=0; i<Solutions.size(); i++){
     JOptionPane.showMessageDialog(null, ""+Solutions.get(i),"Solucion número:"+(i+1),JOptionPane.INFORMATION_MESSAGE);
    }
    int elapsed = (tiempo * 60) - timer.segundos;
            String Telapsed = get_elapsed(elapsed);
   timer.Stop();
    create_stats(ejercicio,Telapsed,0,"No responde");
   if(currentEX < ExcerciseList.size()-1){
      currentEX++;
      JOptionPane.showMessageDialog(null,"Se presentará el siguiente ejercicio","Aviso",JOptionPane.WARNING_MESSAGE);
      load_excercise(ExcerciseList.get(currentEX),tiempo);
   }
   else{
      JOptionPane.showMessageDialog(null,"Ha llegado al final del entrenamiento","Aviso",JOptionPane.INFORMATION_MESSAGE);
      this.getParent().remove(this);
   }
    
    }
            
    
    private boolean check_answer (String text){
      boolean result = false;
     ConectorBD conexion = new ConectorBD();
     conexion.conectar();
     List<String> Respuestas = conexion.GET_SolutionByID(ejercicio);
     for(int i = 0; i < Respuestas.size(); i++){
         if(text.equals(Respuestas.get(i)))
             result = true;
     
     }
     return result;
    }
    
    private String get_elapsed(int secs){
       String H,M,S = null;
        int h = secs/3600;
        int m = secs /60;
        int s = secs % 60;
        
        if(s>=10)
         S = (""+s);
        else
         S=("0"+s);
        
        if(m>=10)
         M = (""+m);
        else
         M=("0"+m);
        
         if(h>=10)
         H = (""+h);
        else
         H=("0"+h);
         
         return H+M+S;
    
    }
    
    public void init_variables(){
    board = new PlayBoard();
    BoardStatus = "";
    timer = new Timer_panel(this);
    Registry = ""; 
    Movimientos = new ArrayList<char[]>();
    Registros = new ArrayList<String>();
    Solutions = new ArrayList<String>();
    IndexMovimientos = 1;
    turno =0;
    solutiontxt.setText("");
    }
    
       
    @Override
    public void mouseClicked(MouseEvent e) {
      
    
    
    }

    @Override
    public void mousePressed(MouseEvent e) {
  
              
        if( e.getSource() == board){
         Graphics g=board.getGraphics();
         int x = e.getX();
         int y = e.getY();
         int i=board.posinv(x);
         int j=board.posinv(y);
         System.out.println("I"+i+"J"+j);
      if (i>=8 || j>=8) {
         return;
      }
      //  do nothing if square is empty
      if (board.piece[j*8+i]==' ') {
         return;
      }
      //  do nothing if square is not one of the
      //  pieces of the color whose turn it is
     if (board.whiteTurn!=board.isWhite(board.piece[j*8+i])) {
         return;
      }
      
      // start dragging a piece      
      board.dragging=true;
      board.temp=false;
      board.dragCol=i;
      board.dragRow=j;
      board.dragPiece=board.piece[j*8+i];
      // highlight the selected square in red
      board.drawSquare(g,i,j,true);
      return;
    }
    
    }

    @Override
    public void mouseReleased(MouseEvent e) {
         if( e.getSource() == board){
     Graphics g=board.getGraphics();
     int x = e.getX();
     int y = e.getY(); 
     int i=board.posinv(x);
      int j=board.posinv(y);
      if (board.dragging) {
         if (board.temp && i>=0 && j<8
                  && !(i==board.dragCol && j==board.dragRow)) {
            // make the temporary move permanent
            board.drawSquare(g,board.tempCol,board.tempRow,false);
            // remove the piece from its old position
            board.piece[board.dragRow*8+board.dragCol]=' ';
             // place piece in its new position
            board.piece[j*8+i]=board.dragPiece;
            board.drawSquare(g,i,j,false);
            if(!board.whiteTurn){
              Registry +=","+board.translate_move(board.dragRow, board.dragCol, j, i, board.dragPiece)+"\n";
            }
            else{
                Registry += ""+board.translate_move(board.dragRow, board.dragCol, j, i, board.dragPiece);
            }
           solutiontxt.setText(Registry);
           Registros.add(Registry);
           Movimientos.add(board.piece.clone());
           IndexMovimientos++;
           if(IndexMovimientos < Movimientos.size()-1){
             for(int X= IndexMovimientos; X<Movimientos.size();X++){
                  Movimientos.remove(X);
                  Registros.remove(X);
             }
           }
            // change the turn
         board.whiteTurn=!board.whiteTurn;
          
         }
         // remove highlighting on old position
         board.drawSquare(g,board.dragCol,board.dragRow,false);
         board.temp=false;
         board.dragging=false;
      }
      return;
    }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
     //
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //Do nothing
    }

    @Override
    public void mouseDragged(MouseEvent e) {
         if( e.getSource() == board){
   //  System.out.print("Mouse dragged");
        Graphics g=board.getGraphics();
       int x = e.getX();
       int y = e.getY();
       int i=board.posinv(x);
       int j=board.posinv(y);
       if (board.dragging) {
         // has nothing changed from last time?
         if (board.temp && i==board.tempCol && j==board.tempRow) {
            return ;
         }
         if (board.temp) {
            // clear the previous temporary move
            board.drawSquare(g,board.tempCol,board.tempRow,false);
            board.temp=false;
         }
         // check for legal move 
         if (board.legalMove(i,j)) {
            // update new position to show the piece
            board.temp=true;
            board.tempCol=i;
            board.tempRow=j;
            if(!board.promotion){
            
            board.drawSquare(g,i,j,true);
            }
            else{
                
             if(!board.whiteTurn){
              Registry +=""+board.translate_move(board.dragRow, board.dragCol, j, i, board.dragPiece);
                }
            else{
                Registry += ","+board.translate_move(board.dragRow, board.dragCol, j, i, board.dragPiece)+"\n";
            }
                solutiontxt.setText(Registry);
                board.promotion=false;
                board.dragging = false;
                
                
                
                board.update(g);
              }
         
         }
      }
      return ;
    }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
       // throw new UnsupportedOperationException("Not supported yet.");
    }
    
      private void RepaintPanelBoard(){
            PanelBoard.setBackground(new java.awt.Color(116, 165, 182));
        PanelBoard.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        PanelBoard.setName("PanelBoard"); // NOI18N
        PanelBoard.setPreferredSize(new java.awt.Dimension(452, 452));

        jLabel2.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel2.setText("8");

        jLabel3.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel3.setText("7");

        jLabel4.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel4.setText("6");

        jLabel5.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel5.setText("5");

        jLabel6.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel6.setText("4");

        jLabel7.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel7.setText("3");

        jLabel8.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel8.setText("2");

        jLabel9.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel9.setText("1");

        jLabel10.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel10.setText("A");

        jLabel11.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel11.setText("B");

        jLabel12.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel12.setText("C");

        jLabel13.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel13.setText("D");

        jLabel14.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel14.setText("E");

        jLabel15.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel15.setText("F");

        jLabel16.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel16.setText("G");

        jLabel17.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel17.setText("H");

        javax.swing.GroupLayout PanelBoardLayout = new javax.swing.GroupLayout(PanelBoard);
        PanelBoard.setLayout(PanelBoardLayout);
        PanelBoardLayout.setHorizontalGroup(
            PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBoardLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelBoardLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel10)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel11)
                        .addGap(45, 45, 45)
                        .addComponent(jLabel12)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel13)
                        .addGap(40, 40, 40)
                        .addComponent(jLabel14)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel15)
                        .addGap(50, 50, 50)
                        .addComponent(jLabel16)
                        .addGap(52, 52, 52)
                        .addComponent(jLabel17)
                        .addContainerGap())
                    .addGroup(PanelBoardLayout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(458, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PanelBoardLayout.createSequentialGroup()
                        .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
        PanelBoardLayout.setVerticalGroup(
            PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBoardLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(30, 30, 30)
                .addComponent(jLabel3)
                .addGap(36, 36, 36)
                .addComponent(jLabel4)
                .addGap(36, 36, 36)
                .addComponent(jLabel5)
                .addGap(35, 35, 35)
                .addComponent(jLabel6)
                .addGap(41, 41, 41)
                .addComponent(jLabel7)
                .addGap(37, 37, 37)
                .addComponent(jLabel8)
                .addGap(44, 44, 44)
                .addComponent(jLabel9)
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15)
                    .addComponent(jLabel16)
                    .addComponent(jLabel17))
                .addGap(17, 17, 17))
        );
     }

    
    
    /** board method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify board code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PanelBoard = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel18 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel19 = new javax.swing.JLabel();
        nametxt = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptiontxt = new javax.swing.JTextArea();
        jLabel22 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        solutiontxt = new javax.swing.JTextArea();
        jLabel23 = new javax.swing.JLabel();
        clockPanel = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        gobackbtn = new javax.swing.JButton();
        helpbtn = new javax.swing.JButton();
        answerbtn = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(870, 517));

        PanelBoard.setBackground(new java.awt.Color(116, 165, 182));
        PanelBoard.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        PanelBoard.setName("PanelBoard"); // NOI18N
        PanelBoard.setPreferredSize(new java.awt.Dimension(452, 452));

        jLabel2.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel2.setText("8");

        jLabel3.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel3.setText("7");

        jLabel4.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel4.setText("6");

        jLabel5.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel5.setText("5");

        jLabel6.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel6.setText("4");

        jLabel7.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel7.setText("3");

        jLabel8.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel8.setText("2");

        jLabel9.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel9.setText("1");

        jLabel10.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel10.setText("A");

        jLabel11.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel11.setText("B");

        jLabel12.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel12.setText("C");

        jLabel13.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel13.setText("D");

        jLabel14.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel14.setText("E");

        jLabel15.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel15.setText("F");

        jLabel16.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel16.setText("G");

        jLabel17.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel17.setText("H");

        javax.swing.GroupLayout PanelBoardLayout = new javax.swing.GroupLayout(PanelBoard);
        PanelBoard.setLayout(PanelBoardLayout);
        PanelBoardLayout.setHorizontalGroup(
            PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBoardLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelBoardLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel10)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel11)
                        .addGap(45, 45, 45)
                        .addComponent(jLabel12)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel13)
                        .addGap(40, 40, 40)
                        .addComponent(jLabel14)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel15)
                        .addGap(50, 50, 50)
                        .addComponent(jLabel16)
                        .addGap(52, 52, 52)
                        .addComponent(jLabel17)
                        .addContainerGap())
                    .addGroup(PanelBoardLayout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(458, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PanelBoardLayout.createSequentialGroup()
                        .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
        PanelBoardLayout.setVerticalGroup(
            PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBoardLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(30, 30, 30)
                .addComponent(jLabel3)
                .addGap(36, 36, 36)
                .addComponent(jLabel4)
                .addGap(36, 36, 36)
                .addComponent(jLabel5)
                .addGap(35, 35, 35)
                .addComponent(jLabel6)
                .addGap(41, 41, 41)
                .addComponent(jLabel7)
                .addGap(37, 37, 37)
                .addComponent(jLabel8)
                .addGap(44, 44, 44)
                .addComponent(jLabel9)
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15)
                    .addComponent(jLabel16)
                    .addComponent(jLabel17))
                .addGap(17, 17, 17))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setToolTipText("Deshacer movimiento");
        jPanel1.setPreferredSize(new java.awt.Dimension(300, 495));

        jLabel18.setFont(new java.awt.Font("Copperplate Gothic Light", 0, 24)); // NOI18N
        jLabel18.setText("TEChess Training Suite");

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel19.setText("Ejercicio:");

        nametxt.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        nametxt.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jLabel21.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel21.setText("Descripción:");

        descriptiontxt.setColumns(20);
        descriptiontxt.setEditable(false);
        descriptiontxt.setRows(5);
        jScrollPane1.setViewportView(descriptiontxt);

        jLabel22.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel22.setText("Solución:");

        solutiontxt.setColumns(20);
        solutiontxt.setEditable(false);
        solutiontxt.setRows(5);
        jScrollPane2.setViewportView(solutiontxt);

        jLabel23.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel23.setText("Tiempo Restante:");

        clockPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        clockPanel.setLayout(new java.awt.GridLayout(1, 0));

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setLayout(new java.awt.GridLayout(1, 0));

        gobackbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/back.png"))); // NOI18N
        gobackbtn.setToolTipText("Volver a configuración");
        gobackbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gobackbtnActionPerformed(evt);
            }
        });
        jPanel3.add(gobackbtn);

        helpbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/help-icon.png"))); // NOI18N
        helpbtn.setToolTipText("Ver respuesta correcta");
        helpbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpbtnActionPerformed(evt);
            }
        });
        jPanel3.add(helpbtn);

        answerbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/ok.png"))); // NOI18N
        answerbtn.setToolTipText("Responder Ejercicio");
        answerbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                answerbtnActionPerformed(evt);
            }
        });
        jPanel3.add(answerbtn);

        jPanel2.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Undo-icon.png"))); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton4);

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Redo-icon.png"))); // NOI18N
        jButton5.setToolTipText("Rehacer movimiento");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton5);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel22)
                        .addContainerGap(280, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel23)
                        .addContainerGap(228, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel19)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 232, Short.MAX_VALUE))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 165, Short.MAX_VALUE))
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                            .addComponent(nametxt, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                            .addComponent(jLabel21, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(445, 445, 445))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel18)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(clockPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(330, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel18, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel19)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(nametxt, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel21)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel22)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel23)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(clockPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(59, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(PanelBoard, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(PanelBoard, javax.swing.GroupLayout.PREFERRED_SIZE, 505, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
  if(IndexMovimientos > 1)
    IndexMovimientos--;
  
 board.piece = Movimientos.get(IndexMovimientos-1).clone();
 solutiontxt.setText(Registros.get(IndexMovimientos-1));
 Registry = Registros.get(IndexMovimientos-1);
 board.whiteTurn = Who_plays(IndexMovimientos);
 board.update(this.getGraphics());
}//GEN-LAST:event_jButton4ActionPerformed

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
 if(IndexMovimientos < Movimientos.size())
     IndexMovimientos++;
 
 board.piece = Movimientos.get(IndexMovimientos-1).clone();
 Registry = Registros.get(IndexMovimientos-1);
 solutiontxt.setText(Registros.get(IndexMovimientos-1));
 board.whiteTurn = Who_plays(IndexMovimientos);
 
 board.update(this.getGraphics());
}//GEN-LAST:event_jButton5ActionPerformed

private void gobackbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gobackbtnActionPerformed
this.getParent().remove(this);
}//GEN-LAST:event_gobackbtnActionPerformed

private void answerbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_answerbtnActionPerformed
       if(!"".equals(solutiontxt.getText())) {
    boolean check_answer = check_answer(solutiontxt.getText());
        int elapsed = (tiempo * 60) - timer.segundos;
        String Telapsed = get_elapsed(elapsed);
   timer.Stop();
   if(check_answer){
   
       create_stats(ejercicio,Telapsed,1,solutiontxt.getText());
       JOptionPane.showMessageDialog(null,"Felicidades ha contestado de manera correcta","TEChess Master",JOptionPane.INFORMATION_MESSAGE);   
   
   }
   else{
   create_stats(ejercicio,Telapsed,0,solutiontxt.getText());
     JOptionPane.showMessageDialog(null,"Ha contestado de manera incorrecta","TEChess Master",JOptionPane.INFORMATION_MESSAGE); 
   
     for(int i=0; i<Solutions.size(); i++){
     JOptionPane.showMessageDialog(null, ""+Solutions.get(i),"Solucion Correcta número:"+(i+1),JOptionPane.INFORMATION_MESSAGE);
      }
     
     
   }
   
   if(currentEX < ExcerciseList.size()-1){
      currentEX++;
      JOptionPane.showMessageDialog(null,"Se presentará el siguiente ejercicio","Aviso",JOptionPane.WARNING_MESSAGE);
      load_excercise(ExcerciseList.get(currentEX),tiempo);
   }
   else{
      JOptionPane.showMessageDialog(null,"Ha llegado al final del entrenamiento","Aviso",JOptionPane.INFORMATION_MESSAGE);
    this.getParent().remove(this);
   }
    
       }
       else{
       JOptionPane.showMessageDialog(null,"La respuesta no debe contener al menos un movimiento","Aviso",JOptionPane.ERROR_MESSAGE);
       
       }    
       
}//GEN-LAST:event_answerbtnActionPerformed

private void helpbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpbtnActionPerformed
        int  ver_respuesta = JOptionPane.showConfirmDialog(null,"Si elige ver las respuestas se registrará como una respuesta erronea\n"
                                        + " en su expediente de entrenamiento \n"
                                        + "Desea Continuar?","Advertencia",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
  if(ver_respuesta == JOptionPane.YES_OPTION){
   for(int i=0; i<Solutions.size(); i++){
     JOptionPane.showMessageDialog(null, ""+Solutions.get(i),"Solucion número:"+(i+1),JOptionPane.INFORMATION_MESSAGE);
    }
   int elapsed = (tiempo * 60) - timer.segundos;
            String Telapsed = get_elapsed(elapsed);
   timer.Stop();
   create_stats(ejercicio,Telapsed,0,"No responde");
   if(currentEX < ExcerciseList.size()-1){
      currentEX++;
      JOptionPane.showMessageDialog(null,"Se presentará el siguiente ejercicio","Aviso",JOptionPane.WARNING_MESSAGE);
      load_excercise(ExcerciseList.get(currentEX),tiempo);
   }
   else{
      JOptionPane.showMessageDialog(null,"Ha llegado al final del entrenamiento","Aviso",JOptionPane.INFORMATION_MESSAGE);
      this.getParent().remove(this);
   }
   
   
  }
    
    
}//GEN-LAST:event_helpbtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel PanelBoard;
    private javax.swing.JButton answerbtn;
    private javax.swing.JPanel clockPanel;
    private javax.swing.JTextArea descriptiontxt;
    private javax.swing.JButton gobackbtn;
    private javax.swing.JButton helpbtn;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel nametxt;
    private javax.swing.JTextArea solutiontxt;
    // End of variables declaration//GEN-END:variables
  
   
}
